package com.example.onlinemusic.Controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/loveMusic")
public class LoveMusicController {
    @Resource
    private LoveMusicMapper loveMusicMapper;

    /**
     * 收藏音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            return new ResponseBodyMessage<>(-1, "请登陆后上传",false);
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        Music music = loveMusicMapper.findLoveMusic(userId,musicId);
        if(music != null){
            return new ResponseBodyMessage<>(-1,"已经收藏该音乐",false);
        }
        if(loveMusicMapper.insertLoveMusic(userId, musicId)){
            return new ResponseBodyMessage<>(200,"收藏成功",true);
        }
        return new ResponseBodyMessage<>(-1, "收藏失败",false);
    }

    /**
     * 查询收藏的音乐
     * @return
     */
    @RequestMapping("/findLoveMusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            return new ResponseBodyMessage<>(-1, "请登陆后查找",null);
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        List<Music> res = null;
        if(musicName == null){
            res = loveMusicMapper.findMusicByUserId(userId);
        }else {
            res = loveMusicMapper.findMusicByKeyAndUID(musicName, userId);
        }
        return new ResponseBodyMessage<>(200,"查询成功",res);
    }
    /**
     * 删除收藏歌曲
     */
    @RequestMapping("/deleteLoveMusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,
                                                        HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            return new ResponseBodyMessage<>(-1, "请登陆后查找",null);
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        int res = loveMusicMapper.deleteLoveMusic(userId,musicId);
        if(res == 1){
            return new ResponseBodyMessage<>(200,"取消收藏成功",true);
        }else {
            return new ResponseBodyMessage<>(-1, "取消收藏失败",false);
        }
    }
}
